using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace Circus.CodeEditor
{
	[DesignerCategory("Code"), ToolboxBitmap(typeof(ColorBox), "Images.ColorBox.bmp")]
	public class ColorBox : ComboBox, IDisposable
	{
		private const int colorWidth = 28;
		private Container components = null;
		private Brush fontBrush;
		[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public new ComboBox.ObjectCollection Items
		{
			get
			{
				return base.Items;
			}
		}
		[Description("Represents text associated with this control.")]
		public new string Text
		{
			get
			{
				return base.Text;
			}
		}
		[Category("Appearence"), Description("Gets or sets curently selected color in the \"ColorBox\".")]
		public Color SelectedColor
		{
			get
			{
				return (base.SelectedItem != null) ? this.GetColor(this.SelectedIndex) : Color.Empty;
			}
			set
			{
				if (value == Color.Empty)
				{
					this.SelectedIndex = 0;
				}
				else
				{
					for (int i = 0; i < this.Items.Count; i++)
					{
						if (string.Compare(value.Name, this.Items[i].ToString(), true) == 0)
						{
							this.SelectedIndex = i;
							break;
						}
					}
				}
			}
		}
		private void InitializeComponent()
		{
			this.components = new Container();
		}
		private void InternalCreate()
		{
			this.fontBrush = new SolidBrush(this.ForeColor);
			base.DropDownStyle = ComboBoxStyle.DropDown;
			base.DrawMode = DrawMode.OwnerDrawFixed;
			this.UpdateColors();
			this.SelectedIndex = 0;
		}
		private void UpdateColors()
		{
			this.Items.Add(StringConsts.EmptyColor);
			Type typeFromHandle = typeof(Color);
			PropertyInfo[] properties = typeFromHandle.GetProperties();
			for (int i = 0; i < properties.Length; i++)
			{
				if (properties[i].PropertyType == typeof(Color))
				{
					this.Items.Add(properties[i].Name);
				}
			}
			typeFromHandle = typeof(SystemColors);
			properties = typeFromHandle.GetProperties();
			for (int i = 0; i < properties.Length; i++)
			{
				if (properties[i].PropertyType == typeof(Color))
				{
					this.Items.Add(properties[i].Name);
				}
			}
		}
		private Color GetColor(int index)
		{
			Color result;
			if (index > 0 && index < this.Items.Count)
			{
				result = Color.FromName(this.Items[index].ToString());
			}
			else
			{
				result = Color.Empty;
			}
			return result;
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this.components != null)
				{
					this.components.Dispose();
				}
				if (this.fontBrush != null)
				{
					this.fontBrush.Dispose();
					this.fontBrush = null;
				}
			}
			base.Dispose(disposing);
		}
		protected override void OnForeColorChanged(EventArgs e)
		{
			base.OnForeColorChanged(e);
			this.fontBrush = new SolidBrush(this.ForeColor);
		}
		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			if (e.Index >= 0 && e.Index < this.Items.Count)
			{
				e.DrawBackground();
				bool flag = (e.State & DrawItemState.Disabled) != DrawItemState.None;
				bool flag2 = (e.State & DrawItemState.Focus) != DrawItemState.None && (e.State & DrawItemState.NoFocusRect) == DrawItemState.None;
				bool flag3 = (e.State & DrawItemState.Selected) != DrawItemState.None;
				if (flag2)
				{
					e.DrawFocusRectangle();
				}
				Brush brush = new SolidBrush(this.GetColor(e.Index));
				try
				{
					string s = (e.Index == 0) ? StringConsts.EmptyColor : this.GetColor(e.Index).Name;
					Rectangle rect = new Rectangle(e.Bounds.Left, e.Bounds.Top, Math.Min(e.Bounds.Width, 28), e.Bounds.Height);
					rect.Inflate(-2, -2);
					Rectangle r = new Rectangle(rect.Right + 1, e.Bounds.Top, e.Bounds.Width - rect.Right - 1, e.Bounds.Height);
					if (e.State == DrawItemState.HotLight)
					{
						e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
					}
					e.Graphics.FillRectangle(brush, rect);
					e.Graphics.DrawRectangle(Pens.Black, rect);
					Brush brush2 = this.fontBrush;
					if (flag)
					{
						brush2 = Brushes.Gray;
					}
					else
					{
						if (flag3)
						{
							brush2 = SystemBrushes.HighlightText;
						}
					}
					e.Graphics.DrawString(s, this.Font, brush2, r);
				}
				finally
				{
					brush.Dispose();
				}
			}
		}
		public ColorBox(IContainer container)
		{
			container.Add(this);
			this.InternalCreate();
			this.InitializeComponent();
		}
		public ColorBox()
		{
			this.InternalCreate();
			this.InitializeComponent();
		}
	}
}
